package com.efast.dmom.aps.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.Station;
import com.efast.dmom.aps.datahandlers.StationHandler;
import com.efast.dmom.aps.service.StationService;
import com.efast.dmom.aps.util.StringVerifyUtil;
@Service
public class StationServiceImpl implements StationService {

	@Autowired
	private StationHandler stationHandler;
	@Override
	public ResultBean save(Station station) {
		ResultBean resultBean = new ResultBean(true,"新增成功！");
		if (!StringUtil.isNotBlank(station.getInnerData().getName())
				&& !StringUtil.isNotBlank(station.getInnerData().getStationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工位名称、工位编码是否填写!");
			return resultBean;
			
		}
		
		if (!StringVerifyUtil.isstring(station.getInnerData().getStationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工位编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (station.getInnerData().getWorkCenterId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工作中心id不能为空!");
			return resultBean;
		}
		Station stationByName = stationHandler.getByName(station.getInnerData().getName());
		Station stationByCode = stationHandler.getByCode(station.getInnerData().getStationCode());
		if (stationByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工位名称不唯一!");
			return resultBean;
		}
		if (stationByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工位编码不唯一!");
			return resultBean;
		}
		stationHandler.save(station);
		return resultBean;
	}

	@Override
	public ResultBean update(Station station) {
		ResultBean resultBean = new ResultBean(true,"编辑成功！");
		Station stationById = stationHandler.getById(station.getInnerData().getId());
		if (stationById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的工位不存在!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(station.getInnerData().getStationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工位编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!station.getInnerData().getStationCode().equals(stationById.getInnerData().getStationCode())
				&& !station.getInnerData().getName().equals(stationById.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工位编码、工位名称不可修改!");
			return resultBean;
		}
		stationHandler.update(station);
		return resultBean;
	}

	@Override
	public ResultBean delete(Station station) {
		ResultBean resultBean = new ResultBean(true,"删除成功！");
		stationHandler.delete(station);
		return resultBean;
	}

	@Override
	public Station getById(long id) {
		Station byId = stationHandler.getById(id);
		
		return byId;
	}

	@Override
	public Station getByName(String name) {
		Station byName = stationHandler.getByName(name);
		return byName;
	}

	@Override
	public Station getByCode(String workshopCode) {
		Station byCode = stationHandler.getByCode(workshopCode);
		return byCode;
	}

}
